package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.mapapi.model.LatLng;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.DotDesignAdapter;
import com.hongxun.design.adapters.MaterialDetailAdapter;
import com.hongxun.design.bean.CodeSetBean;
import com.hongxun.design.bean.Exploration;
import com.hongxun.design.bean.MaterialBean;
import com.hongxun.design.bean.SetPointInfItemBean;
import com.hongxun.design.bean.codeSetFilter;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.CheckClickTime;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ShardPreferencesTool;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.object_properties.MyMessage;
import com.hongxun.design.sqlite.popwind.QuickModelAddPW;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.pickerview.lib.DensityUtil;
import com.hongxun.design.ui.ui_listview.NoScrollListView;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenu;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuCreator;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuItem;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuListView;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.fileutil.SetFileUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;
import com.hongxun.design.utils.httputil.PostParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/5/18.
 * 继续点位设置
 */
public class DotDesignActivity extends BaseActivity implements HeadSecond {
    private String title = "";
    NoScrollListView design_list;
    public static DotDesignAdapter adapter;
    static List<SetPointInfItemBean> data;

    private SwipeRefreshListView codeset_list;
    private List<MaterialBean> dataMater, delList;
    private Handler handler, upExpMaterialHandler;
    private MaterialDetailAdapter detailAdapter;
    private int cposition = -1;
    private HeadSecondControlPanel headsecond_layout;
    //全选删除
    private LinearLayout layoutdel, layoutnor;
    private int count = -1;
    private boolean isLong = false;
    private CheckBox checkBox;
    private LinearLayout layoutMainView;
    //new离线部分数据
    private List<MaterialBean> listMaterials;//点位材料list
    private List<MaterialBean> listModulars;//点位模块list

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dotdesign);
        findBar();
        findView();
        initialization();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MyApp.activity = this;
        mActivity = this;
        Refresh(3);
    }

    private void getIntentData() {
        title = MyApp.currExploration.getName();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 3://加载材料
                        hideWaitIngDialog();
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (dataMater == null) {
                                dataMater = FastJsonUtil.JsonToData_CodeSetMaterial_List((String) msg.obj);
                            } else {
                                dataMater.clear();
                                dataMater.addAll(FastJsonUtil.JsonToData_CodeSetMaterial_List((String) msg.obj));
                            }
                            handler.sendEmptyMessage(4);
//                            upLocalData();
                        } else {
                            codeset_list.setRefreshing(false);
                        }
                        break;
                    case 4:
                        if (detailAdapter == null) {
                            detailAdapter = new MaterialDetailAdapter(dataMater, mActivity, 1);
                            codeset_list.setAdapter(detailAdapter);
                        } else {
                            codeset_list.update();
                        }
                        codeset_list.setRefreshing(false);
                        break;
                    case HttpStringUtil.SUCCESSED://换材料：类型、数量、是否利旧
                    case 2://删除材料
                        final String obj = String.valueOf(msg.obj);
                        if (!FastJsonUtil.JsonToCheck(obj)) {
                            hideWaitIngDialog();
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String(obj));
                        } else {
                            handler.sendEmptyMessageDelayed(1001, 3000);
                        }
                        break;
                    case 1001:
                        codeset_list.update();
                        hideWaitIngDialog();
                        ToastShow.showShort(mActivity, MString.SUCCESSED_HTTP);
                        break;
                    case 6://创建典型组装并添加材料
                        JSONObject object = JSON.parseObject(String.valueOf(msg.obj));
                        if (object.get("code").equals("SUCCESS")) {
                            ToastShow.showShort(mActivity, MString.SUCCESSED_HTTP);
                        } else {
                            ToastShow.showShort(mActivity, "创建典型组装失败");
                        }
                        break;
                    case 7://for循环删除材料
                        if (delList.isEmpty()) {
                            ToastShow.showShort(mActivity, "未选中任何材料");
                            hideWaitIngDialog();
                            return;
                        }
                        count++;
                        if (count < delList.size()) {
                            delMaterial(delList.get(count), 7);
                        } else {
                            count = -1;
                            isLong = false;
                            delList.clear();
                            hideWaitIngDialog();
                            Refresh(3);
                            if (layoutdel.getVisibility() == View.VISIBLE) {
                                layoutnor.setVisibility(View.VISIBLE);
                                layoutdel.setVisibility(View.GONE);
                            }
                        }
                        break;
                    case 8:
                        codeset_list.update();
                        break;
                    case 9:
                        checkBox.setChecked((Boolean) msg.obj);
                        codeset_list.update();
                        break;
                    case 10://单个修改数据时预先显示修改
                        //数据刷新
                        Bundle bundle = msg.getData();
                        double num = bundle.getDouble("1");
                        String old = bundle.getString("2");
                        String spec = bundle.getString("3");
//                        long obj_id = bundle.getLong("4");
                        MaterialBean bean = (MaterialBean) msg.obj;
                        int index = dataMater.indexOf(msg.obj);
                        bean.setNum(num + "");
                        bean.setSpec(spec);
                        bean.setUse_type(old);
                        dataMater.set(index, bean);
                        codeset_list.update();
                        updataLocationData(msg.obj, bean);
                        break;
                }
            }
        };
    }

    //nx更新本地点位材料数据（暂不开启）
    private void upLocalData() {
//        listMaterials.clear();
//        listModulars.clear();
//        for (MaterialBean bean : dataMater) {
//            if (bean.getObj_type().equals("0")) {//材料修改
//                listMaterials.add(bean);
//            } else if (bean.getObj_type().equals("1")) {//模块修改
//                listModulars.add(bean);
//            }
//        }
//        String key1 = MString.nxPointMaterial + MyApp.currExploration.getExploration_id();
//        ShardPreferencesTool.save(getApplicationContext(), key1, FastJsonUtil.beanToJson(listMaterials), 3);
//        String key2 = MString.nxPointModel + MyApp.currExploration.getExploration_id();
//        ShardPreferencesTool.save(getApplicationContext(), key2, FastJsonUtil.beanToJson(listModulars), 3);
    }

    //nx修改点位材料后更新本地数据
    private void updataLocationData(Object obj, MaterialBean bean) {
        if (!MyApp.NetWorkConnect) {
            if (bean.getObj_type().equals("0")) {//材料修改
                int i = listMaterials.indexOf(obj);
                listMaterials.set(i, bean);
                String key = MString.nxPointMaterial + MyApp.currExploration.getExploration_id();
                ShardPreferencesTool.save(getApplicationContext(), key, FastJsonUtil.beanToJson(listMaterials), 3);
            } else if (bean.getObj_type().equals("1")) {//模块修改
                int i = listModulars.indexOf(obj);
                listModulars.set(i, bean);
                String key = MString.nxPointModel + MyApp.currExploration.getExploration_id();
                ShardPreferencesTool.save(getApplicationContext(), key, FastJsonUtil.beanToJson(listModulars), 3);
            }
            hideWaitIngDialog();
        }
    }

    //nx删除点位材料后更新本地数据
    private void delLocationData(MaterialBean bean) {
        if (bean.getObj_type().equals("0")) {//更新点位材料
            listMaterials.remove(bean);
            String key = MString.nxPointMaterial + MyApp.currExploration.getExploration_id();
            ShardPreferencesTool.save(getApplicationContext(), key, FastJsonUtil.beanToJson(listMaterials), 3);
        } else if (bean.getObj_type().equals("1")) {//更新点位模块
            listModulars.remove(bean);
            String key = MString.nxPointModel + MyApp.currExploration.getExploration_id();
            ShardPreferencesTool.save(getApplicationContext(), key, FastJsonUtil.beanToJson(listModulars), 3);
        }
        dataMater.remove(bean);
    }

    @Override
    public void findBar() {
        getIntentData();
        headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.design_head);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean("", View.GONE,
                R.color.bar_right, ListenerSet.back, title, R.color.title, View.VISIBLE,
                IconSourse.BACK + "", View.VISIBLE));
    }

    @Override
    protected void findView() {
        layoutMainView = (LinearLayout) findViewById(R.id.mainView);
        //new
        layoutnor = (LinearLayout) findViewById(R.id.design_view);
        layoutdel = (LinearLayout) findViewById(R.id.design_view2);
        checkBox = (CheckBox) findViewById(R.id.dd_cbox);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String small = "0";
                        if (checkBox.isChecked()) {
                            small = "1";
                        }
                        for (MaterialBean bean : dataMater) {
                            bean.setBig_category("1");
                            bean.setMiddle_category(small);
                        }
                        handler.sendEmptyMessage(8);
                    }
                }).start();
            }
        });
        TextView tvDel = (TextView) findViewById(R.id.dd_tv_del);
        SetTypeface.set(tvDel, IconSourse.DELECT);
        tvDel.append(" 删除");
        delList = new ArrayList<>();
        tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWaitDialog(-1, "处理...");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        delList.clear();
                        for (MaterialBean bean : dataMater) {
                            if (bean.getMiddle_category() != null && bean.getMiddle_category().equals("1")) {
                                delList.add(bean);
                            }
                        }
                        count = -1;
                        handler.sendEmptyMessage(7);
                    }
                }).start();
            }
        });
        //取消
        findViewById(R.id.dd_tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (layoutdel.getVisibility() == View.VISIBLE) {
                    layoutnor.setVisibility(View.VISIBLE);
                    layoutdel.setVisibility(View.GONE);
                }
                for (MaterialBean bean : dataMater) {
                    bean.setBig_category("0");
                    bean.setMiddle_category("0");
                }
                isLong = false;
                codeset_list.update();
            }
        });
        /**设置底部按钮**/
        TextView finish_button = (TextView) findViewById(R.id.design_finish_button);
        TextView continue_button = (TextView) findViewById(R.id.design_continue_button);
        TextView desginSet = (TextView) findViewById(R.id.desgin_set);
        SetTypeface.set(finish_button, IconSourse.SURE);
        SetTypeface.set(continue_button, IconSourse.PHOTOGRAPH);
        SetTypeface.set(desginSet, IconSourse.EDIT);

        finish_button.append(" 直接完成");
        if (MyApp.ReadOnly) {
            finish_button.setBackgroundResource(R.drawable.gray_item_6);
            continue_button.append(" 照片");
        } else {
            desginSet.append(" 速成组装");
            continue_button.append(" 继续拍照");
        }
        design_list = (NoScrollListView) findViewById(R.id.design_list1);
        codeset_list = (SwipeRefreshListView) findViewById(R.id.design_list);
        codeset_list.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Refresh(3);
            }
        });
        addDelect();
        codeset_list.setRefreshing(true);
        upExpMaterialHandler();
        super.findView();
    }

    //下拉
    private void Refresh(int handlercase) {
        if (layoutdel.getVisibility() == View.VISIBLE) {
            layoutdel.setVisibility(View.GONE);
            layoutnor.setVisibility(View.VISIBLE);
        }
        if (MyApp.NetWorkConnect) {
            count = -1;
            isLong = false;
            MyApp.HTTP.getQueryExpCodeset(handler, handlercase, MyApp.currExploration.getExploration_id());
        } else {
            //nx读取本地点位数据
            String key1 = MString.nxPointMaterial + MyApp.currExploration.getExploration_id();
            String pointMaterial = ShardPreferencesTool.read(getApplicationContext(), key1, "", 3);
            listMaterials = JSON.parseArray(pointMaterial, MaterialBean.class);
            if (listMaterials == null) listMaterials = new ArrayList<>();
            String key2 = MString.nxPointModel + MyApp.currExploration.getExploration_id();
            String pointModel = ShardPreferencesTool.read(getApplicationContext(), key2, "", 3);
            listModulars = JSON.parseArray(pointModel, MaterialBean.class);
            if (listModulars == null) listModulars = new ArrayList<>();
            if (dataMater == null) dataMater = new ArrayList<>();
            else dataMater.clear();
            dataMater.addAll(listModulars);
            dataMater.addAll(listMaterials);
            handler.sendEmptyMessage(4);
        }
    }

    //修改踏勘典设材料数量
    private void upExpMaterialHandler() {
        upExpMaterialHandler = new Handler() {
            int num = -1;
            String type;

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            upExpMaterialHandler.sendEmptyMessage(2);
                        }
                        break;
                    case 1:
                        num = msg.arg1;
                        type = msg.obj.toString();
                        break;
                    case 2:
                        if (num == -1) return;
                        dataMater.get(cposition).setNum(num + "");
                        dataMater.get(cposition).setUse_type(type);
                        adapter.notifyDataSetChanged();
                        Refresh(3);
                        break;
                    case HttpStringUtil.ERROR:
                        ToastShow.showShort(mActivity, "修改失败！");
                        break;
                }
            }
        };
    }

    @Override
    protected void initialization() {
        String jsonstring = MyApp.currExploration.getJsonstring();
//        String s = GetFileUtil.getExplorationInfo(MyApp.currExploration.getExploration_id());
//        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject jsonObject = JSON.parseObject(jsonstring);
        data = new ArrayList<>();
        SetPointInfItemBean itemBean1 = new SetPointInfItemBean("典设方案", MString.codeset_id,
                Constant.newStyle1, "未选择", "未选择", false, null);
        if (jsonObject != null) {
            itemBean1.setValue(jsonObject.getString(MString.codeset_name));
            itemBean1.setValueCode(jsonObject.getString(MString.codeset_id));
        } else {
            itemBean1.setValue("");
            itemBean1.setValueCode(MyApp.currExploration.getCodeset_id());
        }
        headsecond_layout.setTitle(title + "：" + jsonObject.getString(MString.number));
//        SetPointInfItemBean itemBean2 = new SetPointInfItemBean("点位材料合计(项)", MString.mCount2,
//                Constant.newStyle2, "0", count + "", false, null);
        data.add(itemBean1);
//        data.add(itemBean2);
        adapter = new DotDesignAdapter(data, this);
        design_list.setAdapter(adapter);
        super.initialization();
    }

    //nx项目点位信息存储
    private void savePoints() {
        String read = ShardPreferencesTool.read(getApplicationContext(), MyApp.currProject.getProject_id(), "", 3);
        List<Exploration> listEP = JSON.parseArray(read, Exploration.class);
        if (listEP == null) listEP = new ArrayList<>();
        boolean isNewPoint = true;
        for (Exploration exp : listEP) {
            if (exp.getExploration_id().equals(MyApp.currExploration.getExploration_id())) {
                isNewPoint = false;
            }
        }
        if (isNewPoint) listEP.add(MyApp.currExploration);
        ShardPreferencesTool.save(getApplicationContext(), MyApp.currProject.getProject_id(), FastJsonUtil.beanToJson(listEP), 3);
    }

    public void setFinish(View v) {
        if (CheckClickTime.back(this)) {
            if (!MyApp.ReadOnly) {
                addFinishHandler();
                data.addAll(MyApp.setPointList);
                if (MyApp.isOnLine) {
                    if (MyApp.NetWorkConnect) {
                        MyApp.HTTP.setConAddExploration(finishHandler, HttpStringUtil.SUCCESSED, data);
                    } else {
                        savePoints();
                        finishHandler.sendEmptyMessage(1);
                    }
                } else {
                    if (!MyApp.NetWorkConnect) {
                        savePoints();
                    }
                    update_SaveAndUpdate(finishHandler, HttpStringUtil.SUCCESSED);
                }
            } else {
                ToastShow.showShort(getApplicationContext(), "此项目已踏勘完成");
            }
        }
    }

    public void setContinue(View v) {
        if (CheckClickTime.back(this)) {
            if (!MyApp.ReadOnly) {
                addContinueHandler();
                data.addAll(MyApp.setPointList);
                if (MyApp.isOnLine) {
                    if (MyApp.NetWorkConnect) {
                        MyApp.HTTP.setConAddExploration(ContinueHandler, HttpStringUtil.SUCCESSED, data);
                    } else {
                        savePoints();
                        ContinueHandler.sendEmptyMessage(1);
                    }
                } else {
                    if (!MyApp.NetWorkConnect) {
                        savePoints();
                    }
                    update_SaveAndUpdate(ContinueHandler, HttpStringUtil.SUCCESSED);
                }
            } else {
                IntentToActivity.intent(mActivity, PhotoGraphActivity.class);
            }
        }
    }

    public void update_SaveAndUpdate(Handler handler, int code) {
        PostParams.setConExploration(data);
        JSONObject jsonObject = JSON.parseObject(MyApp.currExploration.getJsonstring());
        if (jsonObject == null) jsonObject = new JSONObject();
        JSONObject pt = null;
        try {
            pt = JSON.parseObject(jsonObject.getString(MString.pt));
        } catch (Exception e) {
            pt = new JSONObject();
        }
        if (pt == null) pt = new JSONObject();

        Object[] keys = PostParams.map.keySet().toArray();
        for (Object s : keys) {
            if (jsonObject.keySet().contains(s.toString())) {
                jsonObject.put(s.toString(), PostParams.map.get(s.toString()));
            } else if (MString.codeset_id.equals(s) || MString.codeset_name.equals(s)) {
                jsonObject.put(s.toString(), PostParams.map.get(s.toString()));
                MyApp.currExploration.setCodeset_id(PostParams.map.get(s.toString()).toString());
            } else {
                if (!s.toString().equals("exploration_id")) {
                    pt.put(s.toString(), PostParams.map.get(s.toString()));
                    jsonObject.put(MString.pt, pt);
                } else {
                    jsonObject.put(s.toString(), PostParams.map.get(s.toString()));
                }
            }
        }
        jsonObject.put(MString.codeset_name, data.get(0).getValue());
        jsonObject.put(MString.codeset_id, data.get(0).getValueCode());

        MyApp.currExploration.setJsonstring(JSON.toJSONString(jsonObject));
        jsonObject.put(MString.create_time, MyApp.currExploration.getCreate_time());
        SetFileUtil.setExplorationList(MyApp.currExploration, MyApp.currExploration.getPoint_type());
        SetFileUtil.setExplorationInfo(JSON.toJSONString(jsonObject));
        //多一句
        handler.sendMessage(MyMessage.getMessage(code, MString.SUCCESSED_HTTP));
    }

    public static void update_SaveAndUpdate() {
        PostParams.setConExploration(data);
        JSONObject jsonObject = JSON.parseObject(MyApp.currExploration.getJsonstring());
        if (jsonObject == null) jsonObject = new JSONObject();
        JSONObject pt = null;
        try {
            pt = JSON.parseObject(jsonObject.getString(MString.pt));
        } catch (Exception e) {
            pt = new JSONObject();
        }
        if (pt == null) pt = new JSONObject();

        Object[] keys = PostParams.map.keySet().toArray();
        for (Object s : keys) {
            if (jsonObject.keySet().contains(s.toString())) {
                jsonObject.put(s.toString(), PostParams.map.get(s.toString()));
            } else if (MString.codeset_id.equals(s) || MString.codeset_name.equals(s)) {
                jsonObject.put(s.toString(), PostParams.map.get(s.toString()));
                MyApp.currExploration.setCodeset_id(PostParams.map.get(s.toString()).toString());
            } else {
                if (!s.toString().equals("exploration_id")) {
                    pt.put(s.toString(), PostParams.map.get(s.toString()));
                    jsonObject.put(MString.pt, pt);
                } else {
                    jsonObject.put(s.toString(), PostParams.map.get(s.toString()));
                }
            }
        }
        jsonObject.put(MString.codeset_name, data.get(0).getValue());
        jsonObject.put(MString.codeset_id, data.get(0).getValueCode());

        MyApp.currExploration.setJsonstring(JSON.toJSONString(jsonObject));
        jsonObject.put(MString.create_time, MyApp.currExploration.getCreate_time());
        SetFileUtil.setExplorationList(MyApp.currExploration, MyApp.currExploration.getPoint_type());
        SetFileUtil.setExplorationInfo(JSON.toJSONString(jsonObject));
    }

    private Handler finishHandler;
    private Handler ContinueHandler;

    private void addFinishHandler() {
        if (finishHandler == null) finishHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            update_SaveAndUpdate();
                            ToastShow.showShort(mActivity, "修改成功！");
                        } else {
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        finishHandler.sendEmptyMessage(1);
                        break;
                    case 1:
                        if (MyApp.BackActivity == 0) {
                            IntentToActivity.intent(MyApp.activity, ReconnaissanceActivity.class);
                        } else if (MyApp.BackActivity == 1) {
                            IntentToActivity.intent(MyApp.activity, ProjectMapActivity.class);
                        } else if (MyApp.BackActivity == 2) {
                            IntentToActivity.intent(MyApp.activity, ProjectMapSecondActivity.class);
                        }
                        MyApp.BackActivity = 0;
                        MyApp.activity.finish();
                        break;
                    case HttpStringUtil.ERROR:
                        ToastShow.showShort(mActivity, (String) msg.obj);
                        finishHandler.sendEmptyMessage(1);
                        break;

                }
            }
        };
    }

    private void addContinueHandler() {
        if (ContinueHandler == null) ContinueHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (MString.SUCCESSED_HTTP.equals(msg.obj) || FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            update_SaveAndUpdate();
                            ToastShow.showShort(mActivity, "操作成功！");
                        } else {
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        ContinueHandler.sendEmptyMessage(1);
                        break;
                    case 1:
                        IntentToActivity.intent(mActivity, PhotoGraphActivity.class);
                        break;
                    case HttpStringUtil.ERROR:
                        ToastShow.showShort(mActivity, (String) msg.obj);
                        ContinueHandler.sendEmptyMessage(1);
                        break;

                }
            }
        };
    }

    //更新数据
    public static void setValue(String code, Object obj) {
        if (data == null || ((List<CodeSetBean>) obj).size() == 0) return;
        if (code.equals(MString.ChoiceModelData)) {
            CodeSetBean setBean = ((List<CodeSetBean>) obj).get(0);
            SetPointInfItemBean itemBean = adapter.getData().get(adapter.mposition);
            itemBean.setValue(setBean.getName());
            itemBean.setValueCode(setBean.getCodeset_id());
            MyApp.currExploration.setCodeset_id(setBean.getCodeset_id());
            adapter.notifyDataSetChanged();
            //保存典设切换后的点位信息
            update_SaveAndUpdate();
        }

    }

    public static void setValue(LatLng currentPt, String id) {
        adapter.getData().get(0).setLat(currentPt.latitude);
        adapter.getData().get(0).setLon(currentPt.longitude);
        adapter.getData().get(0).setValueid(id);
        adapter.notifyDataSetChanged();
    }

    public static String getFilter() {
        String s = "";
        List<String> stringList = codeSetFilter.getNameCode(MyApp.currExploration.getPt_id());
        for (String ss : stringList) {
            for (SetPointInfItemBean b : data) {
                if (ss.equals(b.getNameCode())) {
                    s += b.getValue() + ",";
                    break;
                }
            }
        }
        if (s.length() > 0) {
            return s.substring(0, s.length() - 1);
        } else {
            return "";
        }
    }

    public void setUpdateva() {
        if (adapter != null) {
            for (SetPointInfItemBean s : adapter.getData()) {
                if (s.getNameCode() != null && (s.getNameCode().equals(MString.wire_model)
                        || s.getNameCode().equals(MString.wire_id))) {
                    s.setValue("");
                    s.setValueCode("");
                    break;
                }
            }
            adapter.notifyDataSetChanged();
        }
    }

    private void addDelect() {
        final SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem item_del = new SwipeMenuItem(getApplicationContext());
                item_del.setWidth(DensityUtil.dip2px(getApplicationContext(), 80));
                item_del.setIcon(R.mipmap.ic_delete);
                item_del.setBackground(R.color.red);
                menu.addMenuItem(item_del);
            }
        };
        codeset_list.setMenuCreator(creator, new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                showWaitDialog(-1, "处理中...");
                delMaterial(dataMater.get(position), 2);
            }
        });
        codeset_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isLong) {
                    //已长按后，单击变更为选择
                    choiceItem(dataMater.get(i));
                } else {
                    //修改数量
                    cposition = i;
                    MyDialog.dialogForUpExpMaterial1(mActivity, dataMater.get(i), handler);
                }
            }
        });
        //长按全部删除处理
        codeset_list.setOnItemLongClicklistener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (!isLong) {
                    if (layoutnor.getVisibility() == View.VISIBLE) {
                        layoutnor.setVisibility(View.GONE);
                        layoutdel.setVisibility(View.VISIBLE);
                    }
                    MaterialBean bean1 = dataMater.get(position);
                    for (MaterialBean bean : dataMater) {
                        bean.setBig_category("1");
                        if (bean.getSpec().equals(bean1.getSpec())) {
                            bean.setMiddle_category("1");
                        } else {
                            bean.setMiddle_category("0");
                        }
                    }
                    isLong = true;
                    codeset_list.update();
                }
                return true;
            }
        });
    }

    private void choiceItem(final MaterialBean bean1) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (MaterialBean bean : dataMater) {
                    bean.setBig_category("1");
                    if (bean.getSpec().equals(bean1.getSpec())) {
                        if (bean1.getMiddle_category() != null && bean1.getMiddle_category().equals("1")) {
                            bean.setMiddle_category("0");
                        } else {
                            bean.setMiddle_category("1");
                        }
                    }
                }
                boolean isAll = true;
                for (MaterialBean bean : dataMater) {
                    if (bean.getMiddle_category() != null && bean.getMiddle_category().equals("0")) {
                        isAll = false;
                    }
                }
                Message message = handler.obtainMessage();
                message.obj = isAll;
                message.what = 9;
                handler.sendMessage(message);
            }
        }).start();
    }

    private void delMaterial(MaterialBean materialBean, int index) {
        if (MyApp.NetWorkConnect) {
            MyApp.HTTP.DelExpMaterial(handler, index, materialBean.getId());
        } else {
            delLocationData(materialBean);
            codeset_list.update();
            if (index == 2) {
                hideWaitIngDialog();
            } else if (index == 7) {
                handler.sendEmptyMessage(7);
            }
        }
    }

    //保存为点设
    public void setDesgin(View view) {
        if (dataMater == null || dataMater.isEmpty()) {
            ToastShow.showShort(this, "点位无任何材料");
            return;
        }
        if (!MyApp.NetWorkConnect) {
            ToastShow.showShort(this, "暂无网络，不支持另存为点设");
            return;
        }
        QuickModelAddPW addPW = new QuickModelAddPW(this, handler);
        addPW.showAtLocation(layoutMainView, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0);
    }
}
